from django.shortcuts import render,HttpResponse,redirect

from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger


# Create your views here.

from test01 import models


user = 'will'
password = '123123'


def index(request,page=1):
    context = {}
    page = request.GET.get('page')
    print(page)
    news_list = models.test.objects.all()

    page_robot = Paginator(news_list,15)
    print(page_robot.num_pages)
    try:
        news_list = page_robot.page(page)
    except EmptyPage:
        news_list = page_robot.page(page_robot.num_pages)
    except PageNotAnInteger:
        news_list = page_robot.page(1)



    context['db_list'] =news_list
    context['numpages'] = list(range(1,page_robot.num_pages+1))
    context['page'] = page
    return render(request,'index.html',context)


def login(request):
    if request.method =='GET':
        return render(request,'login.html')
    if request.method =='POST':
        if request.POST.get('user') == 'will' and request.POST.get('password') =='123123':
            from test01.app import test
            return  redirect('/index')
        else:
            error='error'

        return render(request,'login.html',{'error':error})